﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Trade_HospitalUpdate : System.Web.UI.Page, HZ.MBSM.BLL.IPageDescribe 
{
    private Int64 AccountId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                AccountId = _Account.AccountId;
                if (!this.IsPostBack)
                {
                    HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db(false);
                    Grove.ORM.ObjectOperator oo = db.GetObjectOperator();
                    HZ.MBSM.Model.Hospital Hospital = (HZ.MBSM.Model.Hospital)oo.Retrieve(typeof(HZ.MBSM.Model.Hospital), AccountId);
                    HZ.MBSM.Model.Area Area = (HZ.MBSM.Model.Area)oo.Retrieve(typeof(HZ.MBSM.Model.Area), Hospital.AreaId);

                    if (Hospital != null)
                    {
                        if (Area != null)
                            this.Label1.Text = "<input disabled  type='text' name='AreaName' class='txtbox_150' value='" + Area.AreaName + "' /><input type='text' name='AreaId'   style='display:none; width: 64px;' value='" + Hospital.AreaId + "'>";
                        else
                            this.Label1.Text = "<input  disabled type='text' name='AreaName' class='txtbox_150' /><input type='text' name='AreaId' style='display:none; width: 64px;' >";
                        txtUserCode.Text = SysFun.ToTrim(Hospital.UserCode);
                        HospitalName.Text = Hospital.HospitalName;
                        IsOldLeader.SelectedValue = SysFun.BoolToByte(Hospital.IsOldLeader).ToString();
                        txtLawer.Text = Hospital.Lawer;
                        PostCode.Text = Hospital.PostCode;
                        RegistDate.Text = Hospital.RegistDate.ToShortDateString();
                        RegistDate.Visible = false;
                        Telephone.Text = Hospital.Telephone;
                        Sort.SelectedValue = Hospital.Sort;
                        Address.Text = Hospital.Address;
                        distinction.SelectedValue = Hospital.distinction;
                        txtbedNum.Text = Hospital.bedNum.ToString();
                        txtEmail.Text = Hospital.Email;
                        txtFax.Text = Hospital.Fax;
                        txtmemo1.Text = Hospital.memo1;
                        txtLinkMan.Text = Hospital.LinkMan;
                        txtlistman.Text = Hospital.listman;
                    }
                    
                    oo.Dispose();
                    db.Close();
                    

                }
                string messHtml = GenerMessageHtml();
                if (messHtml != "")
                {
                    Page.RegisterStartupScript("", "<script language='javascript' type='text/javascript'>ListMessageInDiv(\"" + messHtml + "\");</script>");
                }
                else
                {
                    Page.RegisterStartupScript("", "<script language='javascript' type='text/javascript'>closeDIV();</script>");
                }
            }
        }
    }


    /// <summary>
    /// 生成医院重要提示信息HTML样式
    /// </summary>
    /// <returns></returns>
    private string GenerMessageHtml()
    {
        System.Text.StringBuilder sbHtml = new System.Text.StringBuilder("");
        string sql = "select planname,goodsid,productname,inflownum,tradeprice,CompanyName_PS,convert(varchar(10),companyinflowdate,120) as inflowdate from BuyGoodsView_200708 " +
                  "where accountid_yy='" + AccountId + "' and  DealStatus=2  and isnull(ispayment,0)<>1 and DATEDIFF ( day , companyinflowdate , getdate() ) >60 order by planname,CompanyName_PS";
        Db db = new Db();
        System.Data.DataTable dt= db.DataTable(sql);
        db.Close();
        if (dt.Rows.Count > 0)
        {
            System.Text.StringBuilder sbParent = new System.Text.StringBuilder();  
            System.Text.StringBuilder sbChild = new System.Text.StringBuilder();

            sbHtml.Append("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
            
           
            for (int i = 0; i < dt.Rows.Count; i++)
            {   
                string planname = dt.Rows[i]["planname"].ToString();  //记录不重复的采购单名称 
                double totalPrice=0; //记录每个到期采购单的总入库金额

                sbParent.Remove(0, sbParent.Length);
                sbChild.Remove(0, sbChild.Length);
                int j = i;
                int childID = 0;
                for (; j < dt.Rows.Count; j++)
                {
                    string curPlanName=dt.Rows[j]["planname"].ToString();   //当前采购单名称
                    string curGoodsid = dt.Rows[j]["goodsid"].ToString();   //当前流水号
                    string curProductname = dt.Rows[j]["productname"].ToString();   //当前药品名称
                    string curCompany_ps = dt.Rows[j]["CompanyName_PS"].ToString();   //当前配送企业
                    string curInflowDate = dt.Rows[j]["inflowdate"].ToString();   //当前药品入库日期


                    double curPrice = Math.Round(Int32.Parse(dt.Rows[j]["inflownum"].ToString()) * double.Parse(dt.Rows[j]["tradeprice"].ToString()), 2);  //当前入库金额

                    if (curPlanName != planname)
                    {  
                        i = j-1;
                        sbChild.Append("</ol></td></tr>");
                        break;
                    }
                    totalPrice += curPrice;
                    if (j == i)
                    {
                        sbChild.Append("<tr style='display:none;' id='ChiledTR" + i + "'><td style='margin-left:20px;'><ol>");
                        childID = i;
                    }
                    sbChild.Append("<li>" + curGoodsid + "&nbsp;&nbsp;" + curProductname + "&nbsp;&nbsp;<b>" + curCompany_ps + "</b>配送&nbsp;&nbsp;<b>" + curInflowDate + "</b>入库&nbsp;&nbsp;款项:<b>" + Math.Round(curPrice,2) + "</b>元</li>");
                }



                sbHtml.Append("<tr height='30px;' style='font-size:16px;font-weight:bold;' onclick=javascript:OpenOrClose('ChiledTR" + childID + "');><td><img src='../images/point.jpg'/>  <font color='red'>" + planname + "</font>有 <b>" + Math.Round(totalPrice,2) + "</b> 元货款已到60天回款期限</td></tr>");
                sbHtml.Append(sbChild);
                if (j >= dt.Rows.Count)
                {
                    sbChild.Append("</td></tr>");
                    break;
                }
            }
            sbHtml.Append("</table>");
        }
        return sbHtml.ToString();
        //return "";
    }




    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (distinction.SelectedValue == "")
        {
            Response.Write("<script>alert('必须选择医院级别！！！');</script>");
            return;
        }
        HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db(false);
        Grove.ORM.ObjectOperator oo = db.GetObjectOperator();
        HZ.MBSM.Model.Hospital Hospital = new HZ.MBSM.Model.Hospital();

        if (Hospital != null)
        {
            Hospital.AccountId = AccountId;
            Hospital.UserCode = SysFun.ToTrim(txtUserCode.Text);
            Hospital.Address = Address.Text;
            Hospital.AreaId = SysFun.ToInt(Request.Form["AreaId"]);
            Hospital.HospitalName = HospitalName.Text;
            Hospital.IsOldLeader = Convert.ToBoolean(int.Parse(IsOldLeader.SelectedValue));
            Hospital.Lawer = txtLawer.Text;
            Hospital.PostCode = PostCode.Text;
            Hospital.RegistDate = Convert.ToDateTime(RegistDate.Text);
            Hospital.Telephone = Telephone.Text;
            Hospital.Sort = Sort.SelectedValue;
            Hospital.distinction = distinction.SelectedValue;
            Hospital.Email = txtEmail.Text;
            Hospital.bedNum = SysFun.ToInt(txtbedNum.Text);
            Hospital.memo1 = txtmemo1.Text;
            Hospital.LinkMan = txtLinkMan.Text;
            Hospital.listman = txtlistman.Text;
            Hospital.Fax = txtFax.Text;
            oo.Update(Hospital);
            oo.Dispose();
            db.Close();
            Response.Write("<script>alert('保存成功！！！');</script>");
            Response.Redirect("HospitalUpdate.aspx");

        }

        oo.Dispose();
        db.Close();
    }
    #region IPageDescribe 成员

    public string Name
    {
        get { return "医院详细信息"; }
    }

    public string Describe
    {
        get { return ""; }
    }

    public string ParentName
    {
        get { return "医院详细信息"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
            return _AccountType;
        }
    }

    #endregion
}
